const express =require('express')
const Logs=require('../../Schema/logSchema')
const ActionLogs=require('../../Schema/actionSchema')
const ErrorLog= require("../../Schema/errorSchema")
const router= express.Router()

// 登录日志接口
router.get('/logslist',async(req,res)=>{
  const result = await Logs.find({})
  if (!result) {
    res.send({status:1})
    return
  }
  res.send({status:0,result:result})
})

router.get('/logslimit/:page/:size',async(req,res)=>{
  const page=req.params.page
  const size =req.params.size
  const result = await Logs.find({}).skip(parseInt(page) *parseInt(size) ).limit(parseInt(size))
  if (!result) {
    res.send({status:1,result:[]})
    return
  }
  res.send({status:0,result:result})
})

// 操作日志接口
router.get('/actionlist',async(req,res)=>{
     const result = await ActionLogs.find({})
     console.log(result)
     if (!result) {
      res.send({status:0,result:[]})
      return
     }
     res.send({status:1,result:result})
})
router.get('/actionlist/:page/:size',async(req,res)=>{
  const page=req.params.page
  const size =req.params.size
  const result = await ActionLogs.find({}).skip(parseInt(page) *parseInt(size) ).limit(parseInt(size))
  if (!result) {
    res.send({status:1,result:[]})
    return
  }
  res.send({status:0,result:result})
})
router.post('/actionlist',(req,res)=>{
  const userName=req.body.name
  const date=req.body.time
  const passWord=req.body.password
  const operation =req.body.operation
  console.log(userName)
  console.log(date)
  console.log(passWord)
  console.log(operation)
  const newActionLogs = new ActionLogs({
    userName:userName,
    date:date,
    passWord:passWord,
    operation:operation
  })
  newActionLogs.save().then((res=>{
    console.log(res)
  }))
  res.send({status:1})
})
// 错误日志接口
router.get('/errorlist',async(req,res)=>{
  const result = await ErrorLog.find({})
     console.log(result)
     if (!result) {
      res.send({status:0,result:[]})
      return
     }
     res.send({status:1,result:result})
})
router.get('/errorlist/:page/:size',async(req,res)=>{
  const page=req.params.page
  const size =req.params.size
  const result = await ErrorLog.find({}).skip(parseInt(page) *parseInt(size) ).limit(parseInt(size))
  if (!result) {
    res.send({status:1,result:[]})
    return
  }
  res.send({status:0,result:result})
})
router.post('/errorlist',(req,res)=>{
  const userName=req.body.name
  const date=req.body.time
  const passWord=req.body.password
  const error =req.body.error
 const newErrorLog =new ErrorLog({
  userName:userName,
  date:date,
  passWord:passWord,
  error:error
 })
 newErrorLog.save().then((res=>{
  console.log(res)
}))
  res.send({status:1})
})

module.exports=router